package com.escapeicon.bigevent.controller;

import com.escapeicon.bigevent.pojo.entity.Category;
import com.escapeicon.bigevent.pojo.vo.ResultVO;
import com.escapeicon.bigevent.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public ResultVO add(@RequestBody @Validated(Category.Add.class) Category category){
        categoryService.add(category);
        return ResultVO.success();
    }

    @GetMapping
    public ResultVO<List<Category>> get(){
        List<Category> categories = categoryService.list();
        return ResultVO.success(categories);
    }

    @GetMapping("/detail")
    public ResultVO<Category> getDetail(Integer id) {
        Category category = categoryService.findById(id);
        return ResultVO.success(category);
    }

    @PutMapping
    public ResultVO update(@RequestBody @Validated(Category.Update.class) Category category) {
        categoryService.update(category);
        return ResultVO.success();
    }

    @DeleteMapping
    public ResultVO delete(Integer id) {
        categoryService.delete(id);
        return ResultVO.success();
    }
}
